package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.tmall.com.exception.SystemException;
import com.cts.tmall.com.message.RspCode;
import com.cts.tmall.dal.entity.TmallMoneyOff;
import com.cts.tmall.dal.mapper.TmallMoneyOffMapper;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.context.Theme;

import java.util.Map;

@Slf4j
@Service
public class MoneyOffService {

    @Autowired
    private TmallMoneyOffMapper moneyOffMapper;

    public Map<String, Object> queryMoneyOff(Long createUserId,String eventName, String status, Long pageNumb, Long pageSize) {
        LambdaQueryWrapper<TmallMoneyOff> wrapper = Wrappers.<TmallMoneyOff>lambdaQuery()
                .eq(TmallMoneyOff::getCreateUserId,createUserId)
                .eq(StringUtils.isNotBlank(eventName), TmallMoneyOff::getEventName, eventName)
                .eq(StringUtils.isNotBlank(status), TmallMoneyOff::getStatus, status);
        Page<TmallMoneyOff> page = new Page<>(pageNumb, pageSize);
        page = moneyOffMapper.selectPage(page, wrapper);
        return ImmutableMap.<String, Object>builder()
                .put("pageNumb", page.getCurrent())
                .put("pageSize", page.getSize())
                .put("total", page.getTotal())
                .put("data", page.getRecords())
                .build();
    }

    public TmallMoneyOff addMoneyOff(TmallMoneyOff moneyOff) {
        if (StringUtils.isBlank(moneyOff.getEventName())) {
            log.error("活动名称为空");
            throw new SystemException(RspCode.FAILURE_PARAMS, "活动名称为空！");
        }
        if (StringUtils.isBlank(moneyOff.getCanOverlayCoupon())) {
            log.error("允许叠加店铺券为空");
            throw new SystemException(RspCode.FAILURE_PARAMS, "允许叠加店铺券为空");
        }
        moneyOff.setStatus("01");
        moneyOffMapper.insert(moneyOff);
        return moneyOffMapper.selectById(moneyOff.getEventNo());
    }

    public TmallMoneyOff updMoneyOff(TmallMoneyOff moneyOff) {
        if (moneyOff.getEventNo() == null) {
            log.error("活动编号为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "活动编号为空！");
        }
        moneyOffMapper.updateById(moneyOff);
        return moneyOffMapper.selectById(moneyOff.getEventNo());
    }
}
